<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Language
{
    private $_ci;
    var $language;

    public function __construct()
    {
        $this->_ci = & get_instance();
        include APPPATH . 'config/language_TextDataDB.php';
        $this->language = (isset($language_text) ? $language_text : array());
    }

    public function onchangeLanguage()
    {
        $this->save_routes();
        $this->save_metadata();
        $this->save_languagedata();
    }

    public function save_languagedata()
    {
        $this->_ci->load->model('metadata_model');
        $language_data = $this->_ci->metadata_model->get_languagedata();
        $this->_ci->load->helper('file');
        $output = '<?php defined(\'BASEPATH\') OR exit(\'No direct script access allowed\');' . "\n\n";
        $output .= '//save date: ' . date('Y/m/d H:i:s') . "\n";
        if (!empty($language_data)) {
            $languagedata_fold = array();
            foreach ($language_data as $text) {
                $languagedata_fold[$text->lt_data_group][$text->lt_data_key] = $text->ltv_value;
            }
            $output .= '$language_text = ' . var_export($languagedata_fold, true) . ';';
        }
        write_file(APPPATH . 'config/language_TextDataDB.php', $output);
    }

    public function save_config()
    {
        $this->_ci->load->model('settings_model');
        $settings = $this->_ci->settings_model->get_AllSettings(NULL, TRUE);
        $this->_ci->load->helper('file');
        $data = '<?php defined(\'BASEPATH\') OR exit(\'No direct script access allowed\');' . "\n\n";
        $data .= '//save date: ' . date('Y/m/d H:i:s') . "\n";
        $data .= '$test = ' . var_export($settings, true) . ';';
        $data .= '$config = array();if(!empty($test)){foreach($test as $t) $config[$t[\'setting_key\']] = $t[\'setting_value\'];}';
        write_file(APPPATH . 'config/configDB.php', $data);
    }

    public function save_routes()
    {
        $this->_ci->load->model('routes_model');
        $routes = $this->_ci->routes_model->get_routes();

        $this->_ci->load->helper('file');
        $output = '<?php defined(\'BASEPATH\') OR exit(\'No direct script access allowed\');' . "\n\n";
        $data = array();
        if (!empty($routes)) {
            foreach ($routes as $route) {
                $data[] = '$route[\'' . $route->route_name . '\'] = array(' . "\n"
                    . "\t" . '\'key\' => ' . '\'' . $route->route_key . '\',' . "\n"
                    . "\t" . '\'controller\' => ' . '\'' . $route->route_controller . '\',' . "\n"
                    . "\t" . '\'value\' => ' . '\'' . $route->route_value . '\',' . "\n"
                    . "\t" . '\'url\' => ' . '\'' . $route->route_url_struture . '\'' . "\n"
                    . ');';
            }
            $output .= implode("\n", $data);
        }
        write_file(APPPATH . 'config/routesDB.php', $output);
    }

    public function save_metadata()
    {
        $this->_ci->load->model('metadata_model');
        $metadata = $this->_ci->metadata_model->get_metadata();
        $this->_ci->load->helper('file');
        $output = '<?php defined(\'BASEPATH\') OR exit(\'No direct script access allowed\');' . "\n\n";
        $data = array();
        if (!empty($metadata)) {
            $metadata_fold = array();
            foreach ($metadata as $meta) {
                $metadata_fold[$meta->meta_data_group][] = $meta;
            }
            foreach ($metadata_fold as $k => $meta) {
                $str = '$metadata[\'' . $k . '\'] = array(' . "\n";
                foreach ($meta as $m) {
                    $str .= "\t" . '\'' . $m->meta_data_key . '\' => ' . '\'' . $this->_ci->db->escape_str($m->metadata_value) . '\',' . "\n";
                }
                $str .= ');';
                $data[] = $str;
            }
            $output .= implode("\n", $data);
        }
        write_file(APPPATH . 'config/metadataDB.php', $output);
    }

    public function generate_robots()
    {
        $text = "User-agent: * " . "\n" . "Allow: /" . "\n\n" . 'Sitemap: ' . base_url('sitemap.xml');
        $this->_ci->load->helper('file');
        write_file('robots.txt', $text);
    }

    public function save_ads()
    {
        $this->_ci->load->model('ads_model');
        $this->_ci->load->helper('file');
        $ads = (array)$this->_ci->ads_model->get_ads(array('array' => true));
        $new_ads = array();
        if ($ads) {
            foreach ($ads as $fd) {
                $new_ads[$fd['ad_seq_id']] = $fd;
            }
        }

        $data = '<?php defined(\'BASEPATH\') OR exit(\'No direct script access allowed\');' . "\n\n";
        $data .= '//save date: ' . date('Y/m/d H:i:s') . "\n";
        $data .= '$ads = ' . var_export($new_ads, true) . ';';
        write_file(APPPATH . 'config/adsDB.php', $data);
    }

    public function get_ad($id)
    {
        include APPPATH . 'config/adsDB.php';
        if (isset($ads[$id]))
            return $ads[$id]['ad_code'];
        return '';
    }

    public function get_text($text, $group)
    {
        if (isset($this->language[$group][$text]))
            return $this->language[$group][$text];
        return $text;
    }
}